import { onMounted, Ref, ref } from 'vue'
import { listTypes, Type } from '/@/util/api';

export default function getAllTypes() {

    let allTypes: Ref<Array<Type>> = ref([]);
    onMounted(() => {
        console.log(allTypes);
        getAllTypes();
    });
    function getAllTypes() {
        // 获取所有类型
        let sessionAllTypes = JSON.parse(
            window.sessionStorage.getItem("all-types") || "[]"
        ); // 所有类型
        if (sessionAllTypes.length === 0) {
            listTypes().then((res) => {
                if (res.code === 0) {
                    allTypes.value = res.data;
                    window.sessionStorage.setItem(
                        "all-types",
                        JSON.stringify(res.data)
                    );
                }
            });
        } else {
            allTypes.value = sessionAllTypes;
        }
    }
    return {
        allTypes,
    };
}